package com.jetinno.file;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.ImageView;

import java.io.File;

/**
 * Created by zhangyuncai on 2021/11/12.
 */
public class TextActivity extends FragmentActivity {
    private String TAG = "TextActivity";


    private ImageView iv_text_close;
    private Handler mainHandler = new Handler(Looper.getMainLooper());
    private File file;
    private Fragment childFragment = null;

    public File getFile() {
        return file;
    }

    public Context getContext() {
        return this;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_text);

        String brand = Build.BRAND;
        Log.e(TAG, "brand:" + brand);
        if (!TextUtils.equals(brand, "UniWin_M190")&&!TextUtils.equals(brand, "rockchip")) {
            FileTools.showToast(getContext(), "The mainboard is not supported");
            finish();
            return;
        }


        iv_text_close = findViewById(R.id.iv_text_close);
        iv_text_close.setOnClickListener(v -> {
            close();
        });


        Intent intent = this.getIntent();
        Uri uri = intent.getData();
        //内部存储 /storage/emulated/0/Jetinno/Config/canister_config.json
        //U盘存储 /storage/usb(id=0C72-0CD4)/3566/Jetinno/Config/canister_config.json
        if (uri != null && "file".equals(uri.getScheme())) {
            // 处理file:// URI
            String decodedPath = Uri.decode(uri.getEncodedPath());
            file = new File(decodedPath);
        }

        if (file == null || !file.exists()) {
            FileTools.showToast(getContext(), "Cannot edit this file");
            finish();
            return;
        }

        childFragment = new TxtFragment();
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.fl_text_container, childFragment)
                .commit();

        findViewById(R.id.rv_file_title).setOnClickListener(v -> {
            View view = getWindow().peekDecorView();
            if (view != null) {
                InputMethodManager inputmanger = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
                inputmanger.hideSoftInputFromWindow(view.getWindowToken(), 0);
            }
        });


    }

    private void close() {
        new AlertDialog.Builder(this)
                .setMessage("Save?")
                .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        if (childFragment instanceof TxtFragment) {
                            TxtFragment txtFragment = (TxtFragment) childFragment;
                            txtFragment.close();
                        } else {
                            Log.i(TAG, "Yes");
                            finish();
                        }
                    }
                })
                .setNegativeButton("No", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Log.i(TAG, "No");
                        finish();
                    }
                })
                .create()
                .show();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            close();
        }
        return false;
    }

}
